<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="课程名字"></el-input>
    </el-form-item>
    <el-form-item label="课程图片地址" prop="logo">
      <el-input v-model="dataForm.logo" placeholder="课程图片地址"></el-input>
    </el-form-item>
    <el-form-item label="课程介绍" prop="descript">
      <el-input v-model="dataForm.descript" placeholder="课程介绍"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="检索首字母" prop="firstLetter">
      <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
    </el-form-item>
    <el-form-item label="课程热度" prop="courseHeat">
      <el-input v-model="dataForm.courseHeat" placeholder="课程热度"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：禁用   1：正常" prop="isStatus">
      <el-input v-model="dataForm.isStatus" placeholder="状态  0：禁用   1：正常"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          courseId: 0,
          name: '',
          logo: '',
          descript: '',
          sort: '',
          firstLetter: '',
          courseHeat: '',
          isStatus: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          name: [
            { required: true, message: '课程名字不能为空', trigger: 'blur' }
          ],
          logo: [
            { required: true, message: '课程图片地址不能为空', trigger: 'blur' }
          ],
          descript: [
            { required: true, message: '课程介绍不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          firstLetter: [
            { required: true, message: '检索首字母不能为空', trigger: 'blur' }
          ],
          courseHeat: [
            { required: true, message: '课程热度不能为空', trigger: 'blur' }
          ],
          isStatus: [
            { required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.courseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.courseId) {
            this.$http({
              url: this.$http.adornUrl(`/ly/classcourse/info/${this.dataForm.courseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.classCourse.name
                this.dataForm.logo = data.classCourse.logo
                this.dataForm.descript = data.classCourse.descript
                this.dataForm.sort = data.classCourse.sort
                this.dataForm.firstLetter = data.classCourse.firstLetter
                this.dataForm.courseHeat = data.classCourse.courseHeat
                this.dataForm.isStatus = data.classCourse.isStatus
                this.dataForm.gmtCreate = data.classCourse.gmtCreate
                this.dataForm.gmtModified = data.classCourse.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ly/classcourse/${!this.dataForm.courseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'courseId': this.dataForm.courseId || undefined,
                'name': this.dataForm.name,
                'logo': this.dataForm.logo,
                'descript': this.dataForm.descript,
                'sort': this.dataForm.sort,
                'firstLetter': this.dataForm.firstLetter,
                'courseHeat': this.dataForm.courseHeat,
                'isStatus': this.dataForm.isStatus,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
